package lib;

import view.BillCrumb;
import view.BillListing;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class HorizontalIconName extends HorizontalFieldManager implements FocusChangeListener
{

	NullField dummy;
	HorizontalFieldManager pane;
	public HorizontalIconName(Bitmap img, String label)
	{
		this(img, label, Field.FIELD_HCENTER | Field.USE_ALL_WIDTH);
	}
	
	public HorizontalIconName(Bitmap img, String label, long style)
	{
		super(style);
		pane = new HorizontalFieldManager(style | FOCUSABLE)
		{
			protected void sublayout(int width, int height) 
			{
				int fieldWidth = getField(0).getPreferredWidth() + getField(1).getPreferredWidth() + 5;
				
		        setPositionChild( getField(0), Graphics.getScreenWidth()/2 - fieldWidth/2, 0);
		        layoutChild( getField(0), 
		            getField(0).getPreferredWidth(), 
		            getField(0).getPreferredHeight());

		        setPositionChild( getField(1), Graphics.getScreenWidth()/2 - fieldWidth/2 +  getField(0).getPreferredWidth() + 5, 0);
		        layoutChild(
		            getField(1), 
		            getField(1).getPreferredWidth(), 
		            getField(1).getPreferredHeight());    

		        setExtent(width, 55);
		    }
		};
		BitmapField bitmap = new BitmapField(img);
		pane.add(bitmap);
		LabelField labelField = new LabelField(label);
		pane.add(labelField);
		
		pane.setPadding(new XYEdges(5,5,5,5));
		dummy = new NullField(FOCUSABLE);
		dummy.setFocusListener(this);
		pane.add(dummy);
		
		pane.setBackground(BackgroundFactory.createSolidBackground(Color.CADETBLUE));
		pane.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,0,1,0), Border.STYLE_SOLID));
		pane.setFocusListener(this);
		super.add(pane);
	}
	
	public void add(Field f)
	{
		// Do Nothing
	}
	
	public void focusChanged(Field field, int eventType) 
	{
		if(dummy.isFocus() || pane.isFocus())
			pane.setBackground(BillCrumb.FOCUSBACKGROUND);
		else
			pane.setBackground(BackgroundFactory.createSolidBackground(Color.CADETBLUE));
	}      
}
